<?php
class Categoria extends Doctrine_Record {
    public function setTableDefinition() {
        $this->setTableName('categoria');
        $this->hasColumn('id', 'integer', 3, array('primary' => true, 'unsigned'=>true, 'autoincrement'=>true));
        $this->hasColumn('value', 'string', 255);
        $this->hasColumn('id_banner', 'integer', 8, array('unsigned'=>true));
    }
    
    public function setUp(){
        $this->hasOne('Imagen as banner',array(
            'local'=>'id_banner',
            'foreign'=>'id'
        ));
        $this->hasMany('Visita as visitas', array(
            'local' => 'id',
            'foreign' => 'id_categoria'
        ));
        $this->hasMany('Publicidad as publicidades', array(
            'local' => 'id_categoria',
            'foreign' => 'id_publicidad',
            'refClass' => 'RelPublicidadCategoria'
        ));
       //behaviors
        $this->actAs('Sluggable', array('fields'=>array('value'),'unique'=>true,'canUpdate'=>true,'name'=>'slug'));
        $this->actAs('Timestampable', array('created'=>array('disabled'=>true)));
    }
    
    public function hasPublicaciones() {
        return Doctrine_Query::create()
                ->select('count(p.id)')
                ->from('Publicacion p')
                ->innerJoin('p.categoria c WITH c.id = '.$this->id)
                ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR);
    }
    
    public static function toSelect ($objeto=false) {
        $categoriaId = ($objeto)?$objeto->categoria->id:0;
        //$categorias = Doctrine::getTable('categoria')->findAll(Doctrine::HYDRATE_ARRAY);
        $categorias = Doctrine_Query::create()
                ->select('c.*')
                ->from('Categoria c')
                ->whereNotIn('c.slug', array('prensa', 'home', 'about-me'))
                ->execute(array(), Doctrine::HYDRATE_ARRAY)
                ;
        $html = '<select class="dt-inputarea" data-value="'.$categoriaId.'"  id="selectCategoria" name="categoria">';
            foreach ($categorias as $categoria) {
                $selected = ($categoria['id'] == $categoriaId)?' selected="selected"':'';
                $html .= '<option value="' . $categoria['id'] . '"' . $selected . '>' . $categoria['value'] . '</option>';
            }
        $html .= '</select>';
        return $html;
    }
    
    public static function toCheckbox ($publicidad=false) {
        $html = '';
        $categorias = Doctrine_Query::create()->select('c.*')->from('Categoria c')->where('c.slug <> "about-me"')->execute(array(), Doctrine::HYDRATE_ARRAY);
        //$categorias = Doctrine::getTable('categoria')->findAll(Doctrine::HYDRATE_ARRAY);
        if ($publicidad) {
            $ids = Doctrine_Query::create()
                    ->select('c.id')
                    ->from('Categoria c')
                    ->innerJoin('c.publicidades p WITH p.id = '.$publicidad->id)
                    ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR);
            if (!is_array($ids)) $ids = array($ids);
        } else {
            $ids = array();
        }
        for ($i=0, $l=count($categorias); $i<$l; $i++) {
            $checked = (in_array($categorias[$i]['id'], $ids))?' checked="checked"':'';
            $html .= '
                <div class="col col-md-4">
                    <label class="checkbox">
                        <input value="'.$categorias[$i]['id'].'" type="checkbox" name="categorias[]"'.$checked.'>
                        <i></i>'.$categorias[$i]['value'].'
                    </label>
                </div>
            ';
        }
        return $html;
    }
}
?>